package com.perfect.controller;

import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.query.ShopQuery;
import com.perfect.service.ShopService;
import com.perfect.vo.Shop;
import com.perfect.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("shop")
public class ShopController extends BaseController {
    @Resource
    private ShopService shopService;

    /**
     * 返回数据列表
     * @param query
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> ListShop(ShopQuery query){
        return shopService.ListShop(query);
    }

    /**
     * 返回审核数据列表
     * @param query
     * @return
     */
    @RequestMapping("listAudit")
    @ResponseBody
    public Map<String,Object> ListAudit(ShopQuery query){
        System.out.println("--------------------------");
        System.out.println(query.getShopName());
        return shopService.ListAudit(query);
    }

    /**
     * 进入店铺列表界面
     * @param query
     * @return
     */
    @RequestMapping("index")
    public String index(ShopQuery query){
        return "merchant/index";
    }



    /**
     * 打开iframe界面
     * @param query
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("toShopPage")
    public String toShopPage(ShopQuery query, Integer id, HttpServletRequest request){
        if(id != null){
            Shop shop = shopService.selectByPrimaryKey(id);
            request.setAttribute("shop",shop);
        }
        return "merchant/add_update";
    }

    /**
     * 执行添加方法
     * @param shop
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addShop(Shop shop){
        shopService.addShop(shop);
        return success("添加成功");
    }

    /**
     * 执行更新方法
     * @param shop
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateShop(Shop shop){
        shopService.updateShop(shop);
        return success("修改成功");
    }


    /**
     * 执行删除方法
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteShop(Integer[] ids){
        shopService.deleteShop(ids);
        return success("删除成功");
    }

    @GetMapping ("queryAllMerchant")
    @ResponseBody
    public List<User> queryAllMerchant(){
        return shopService.queryAllMerchant();
    }
}
